﻿/********************************************************************************/
// <copyright file="AdminService.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="26/12/2012 3:08:30 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using JLL.China.Model;
using JLL.China.Infrastructure.RepositoryFramework;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.Admin;
using JLL.China.Model.DTO;
using System.Security.Cryptography;
using System.Web.Security;
using System.Text;

namespace JLL.China.Infrastructure.Services
{
    public static class AdminService
    {
        private static IAdminRepository repository;

        static AdminService()
        {
            repository = RepositoryFactory.GetRepository<IAdminRepository, Model.Admin, Guid>(new LINQ2SQLUnitOfWork<Guid>()); 
        }

        public static IList<Admin> GetAllAdmins()
        {
            return repository.FindAll();
        }

        public static Admin GetAdmin(string username)
        {
            return repository.FindByUsername(username);
        }
        public static Admin GetAdmin(Guid id)
        {
            return repository.FindBy(id);
        }

        public static IList<Model.Admin> FindAdmin(AdminSearchCriteria searchCriteria)
        {
            return repository.Search(searchCriteria);
        }

        public static int CountAdmins(AdminSearchCriteria searchCriteria)
        {
            return repository.CountAdmins(searchCriteria);
        }

        public static bool ValidateLogin(string loginName, string password)
        {
            string sha1pwd = CreatePasswordHash(password, GetSalt(loginName));
            return repository.ValidateLogin(loginName, sha1pwd);
        }

        public static void CreateAdmin(Admin admin)
        {
            admin.Password = CreatePasswordHash(admin.Password, admin.ID.ToString("N"));
            repository.Add(admin);
        }

        public static void UpdateAdmin(Admin admin)
        {
            Admin existAdmin = GetAdmin(admin.ID);
            if (admin.Password == null)
                admin.Password = existAdmin.Password;
            else
                admin.Password = AdminService.CreatePasswordHash(admin.Password, admin.ID.ToString("N"));
            repository[admin.ID] = admin;
        }

        public static void DeleteAdmin(Admin admin)
        {
            repository.Remove(admin);
        }

        public static void CommitChanges()
        {
            repository.UnitOfWork.Commit();
        }

        public static IList<Model.Module> FindAllModules()
        {
            return repository.FindAllModules();
        }

        public static Model.Module FindModuleById(int key)
        {
            return repository.FindModuleById(key);
        }

        public static void ChangeStatusSeleted(string selectedIDs, bool status)
        {
            repository.ChangeStatusSeleted(selectedIDs,status);
        }

        public static void RemoveSeleted(string selectedIDs)
        {
            repository.RemoveSeleted(selectedIDs);
        }



        #region Private Methods

        public static string CreatePasswordHash(string password, string salt)
        {
            var snp = password + salt;

            HashAlgorithm sha256 = new SHA256Managed();

            var hashed = sha256.ComputeHash(Encoding.UTF8.GetBytes(snp));

            return Convert.ToBase64String(hashed);
        }

        public static string GetSalt(string loginName)
        {
            var acc = GetAdmin(loginName);
            if (acc != null)
            {
                return acc.ID.ToString("N");
            }
            return string.Empty;
        }



        #endregion
    }
}
